<?php
/**
 * @author: Mark Gil M. Libres
 * @email: markglibres@yahoo.com,mglibres@dwebsite.com
 * @on: Jan 22, 2008
 *
 */
  Zend_Loader::loadClass("BaseControllerAuth");

  class TestController extends BaseControllerAuth
  {
  	public function initialize()
  	{
  		$this->setAuthenticationController("index");
  		$this->byPassAuth("index");
		$this->byPassAuth("timeout");
		$this->byPassAuth("getajak");
		$this->byPassAuth("red");
		$this->byPassAuth("createsaccdb");
		$this->byPassAuth("newsaccdb");
		$this->byPassAuth("lloyd");
  	}
	
	public function timeoutAction()
	{
		$session = new Zend_Session_Namespace("TESTSESSION");
		$session->testvalue = "the quick brown fox";
		echo date("m/d/Y h:i:s A");
		echo " - ".$session->testvalue;
		$this->setLayout("popup");
		$this->render();
		
	}
	
	//ajax call
	public function getajakAction()
	{
		$auth = BaseAuth::getInstance();
		$userId = $auth->id;
		$session = new Zend_Session_Namespace("TESTSESSION");
		$testvalue = $session->testvalue;
		$this->assign("testvalue", $testvalue);
		$this->assign("userId", $userId);
		$this->assign("thedate", date("m/d/Y h:i:s A"));
		$this->assign("serverip", getenv("SERVER_ADDR"));
		$this->setLayout("empty");
		$this->render();
	}
	
	public function newsaccdbAction()
	{
		$this->setLayout("popup");
		$this->render();
	}
	
	public function createsaccdbAction()
	{
		$pass = $this->getPost("pass");
		$customer = $this->getPost("customer");
		if($pass == "arc_sacc"){
			if($customer){
				echo "Creating db for customer $customer.<br>";
				//echo 'is_file(/usr/bin/sudo) : '; var_dump(is_file('/usr/bin/sudo'));
				system('/usr/bin/sudo /var/www/tools/createDbSacc.php ' . $customer);
				echo "<br>";
				echo "<br>DONE.";
			}
		}
		
	}
	
	public function schedpayAction()
	{
		Zend_Loader::loadClass("PaymentUtility");
		//PaymentUtility::processScheduledPayments();
	}
  	
  	public function indexAction()
  	{
		phpinfo();
  		
  	}
	
	
	public function fixcarddataAction()
	{
		
		Zend_Loader::loadClass("CardPaymentUtility");
		Zend_Loader::loadClass("PayerCardData");
		
		$payerCardData = new PayerCardData();
		$pass = $this->getParam("pass");
		if($pass == 'arc_sacc'){
			//$query = "select  payer_card_id, aes_decrypt(card_data, concat(users.cc_key,'" . CardPaymentUtility::CC_DATA_SALT  . "')) as card_num
			//		from payer_card_data INNER JOIN users ON users.id = payer_card_data.user_id where users.id = 1";
					
					
			$query = "	select payer_card_id,  pc.payer_id, fac.family_account_id, 
					aes_decrypt(card_data, concat(users.cc_key,'" . CardPaymentUtility::CC_DATA_SALT  . "' )) as card_num 
					from payer_card_data 
					INNER JOIN users ON users.id = payer_card_data.user_id 
					INNER JOIN payer_cards pc ON pc.id = payer_card_id
					INNER JOIN family_accounts_payers fac ON fac.payer_id = pc.payer_id
					where users.id = 1 ";
					
			$db = DBCon::instance();
			//echo $query;
			$result = $db->executeQuery($query);
			echo "<pre>"; print_r($result); echo "</pre>";
			if($result){
				foreach($result as $row){
					if($row['card_num']){
						$payerCardData->updateCardDataForAdmins($row['payer_card_id'], $row['card_num']);
						echo "updated ".$row['payer_card_id'] ." - " . $row['card_num'] . "<br>" ;
						$payerCardData->updateCardDataForNonAdmins($row['payer_card_id'], $row['card_num']);
						$payerCardData->updateCardDataPerFamilyAccount($row['payer_card_id'], $row['card_num'], $row['family_account_id']);
					}
				}
			}
			
			
		}
	}
	
	public function fixeddataAction()
	{
		//return;
		Zend_Loader::loadClass("AccountingJournal");
		Zend_Loader::loadClass("AccountingInvoice");
		Zend_Loader::loadClass("AccountingPayment");
		Zend_Loader::loadClass("AccountingPaymentInvoice");
		Zend_Loader::loadClass("AccountingJournalRegistration");
		Zend_Loader::loadClass("QueryCreator");
		
		$pass = $this->getParam("pass");
		
		if($pass == 'arc_sacc'){
			$query = new QueryCreator();
			$query->addSelect(" d.id,d.accounting_journal_id,d.registration_id,d.registration_month_id,d.accounting_invoice_id,amount ");
			$query->addFrom(AccountingPayment::_tablename . " a ");
			$query->addJoin(" left join ". AccountingPaymentInvoice::_tablename . "  b on a.id =b.accounting_payment_id ");
			$query->addJoin(" left join ". AccountingJournal::_tablename . " c on c.id = a.journal_id ");
			$query->addJoin(" left join ". AccountingJournalRegistration::_tablename . " d on d.accounting_journal_id = a.journal_id ");
			$query->addWhere(" c.cancelled_date is null ");
			$query->addOrderBy("d.id");
			$db = DBCon::instance();
			$result = $db->executeQuery($query->createSQL());
			$data = array();
			$query2 = new QueryCreator();
			$count = 0;
			foreach($result as $key => $value){
				$count = $count + 1;
				if($value['accounting_invoice_id']) {
					$query2->clear();
					$query2->addSelect("b.*");
					$query2->addFrom(AccountingInvoice::_tablename . " a ");
					$query2->addJoin(" left join ". AccountingJournalRegistration::_tablename . " b on a.journal_id = b.accounting_journal_id ");
					$query2->addJoin(" Left join ". AccountingJournal::_tablename . " c on c.id = a.journal_id ");
					$query2->addWhere(" b.accounting_invoice_id = ". $value['accounting_invoice_id'] . " and cancelled_date is null" ) ;
					$db1 = DBCon::instance();
					$result2 = $db1->executeQuery($query2->createSQL());
					$total_amount = 0;
					foreach($result2 as $keyx => $valuex){
						$total_amount = $total_amount + $valuex['amount'];
					}
					if($value['amount'] == $total_amount && count($result2) > 1 ) {
						$data[$count] = $value;
						$data[$count]['invoice'] = $result2;
					}
				}
			}
			
			$journal_reg = new AccountingJournalRegistration();
			
			foreach($data as $keyitem => $item) {
				$line_id = $item['id'];
				
				$journal_id = $item['accounting_journal_id'];
				echo $journal_id;
				foreach($item['invoice'] as $invoicekey => $invoice) {
					$journal_reg->clear();
					$journal_reg->registration_id = $invoice['registration_id'];
					$journal_reg->registration_month_id = $invoice['registration_month_id'];
					$journal_reg->accounting_invoice_id = $invoice['accounting_invoice_id'];
					$journal_reg->amount = $invoice['amount'];
					$journal_reg->accounting_journal_id = $journal_id;
					$journal_reg->save();
				
				}
				
				$journal_reg->clear();
				$journal_reg->loadWhere('id = '. $line_id );
				$journal_reg->delete(true);
				
			}
			echo "<pre>"; print_r($data); echo "</pre>";
		}
		
	}
	
	public function fixedaccountAction()
	{
		return;
		Zend_loader::loadClass("Registration");
		Zend_loader::loadClass("RegistrationMonth");
		Zend_loader::loadClass("AccountingJournal");
		Zend_loader::loadClass("AccountingInvoice");
		Zend_loader::loadClass("AccountingJournalRegistration");
		Zend_Loader::loadClass("AccountingJournalItem");
		Zend_Loader::loadClass("RegistrationLog");
		Zend_Loader::loadClass("FamilyAccountPayer");
		Zend_Loader::loadClass("CodesUtility");
		Zend_Loader::loadClass("QueryCreator");
		Zend_Loader::loadClass("AccountCodes");
		
		$id = $this->getParam("id");
		$pass = $this->getParam("pass");
		
		if($pass == 'arc_sacc'){
		
			$accountCodes = new AccountCodes();
			$accountCodesArray = $accountCodes->getAccountCodesArray("short_name", "account_code");
		
			$query = new QueryCreator();
			$query->addSelect("a.cost,a.id,b.id as registration_month_id,b.cost as month_cost");
			$query->addSelect(" (select edited_on from ". RegistrationLog::_tablename ." c where c.registration_id = a.id limit 1 ) as reg_date ");
			$query->addFrom(Registration::_tablename . " a ");
			$query->addJoin("LEFT JOIN ". RegistrationMonth::_tablename . " b on a.id = b.registration_id ");
			$query->addWhere(" a.family_account_id = ". $id );
			$query->addOrderBy("a.id,b.id");
			$db = DBCon::instance();
			$result = $db->executeQuery($query->createSQL());
			
			$payer = new FamilyAccountPayer();
			$payer->loadWhere("family_account_id = ". $id );
			
			$journal = new AccountingJournal();
			$invoice = new AccountingInvoice();
			$registration = new AccountingJournalRegistration();
			$journal_item = new AccountingJournalItem();
			$registration_id = 0;
			$regcost = 0;
			foreach($result as $key => $value){
				// create invoice
				if($registration_id != $value['id']) {
				
					$regcost = $value['cost'];
					$journal->clear();
					$journal->transaction_type = 2;
					$journal->transaction_date = $value['reg_date'];
					$journal->posted = 1;
					$journal->family_account_id = $id;
					$journal->save();
					
					$invoice->clear();
					$invoice->journal_id = $journal->id;
					$invoice->payer_id = $payer->payer_id;
					$invoice->date_created = date("Y-m-d H:i:s");
					$invoice->last_updated = date("Y-m-d H:i:s");
					$invoice->date_due = $value['reg_date'];
					$invoice->save();
					
					$invoice->invoice_number =  CodesUtility::STARTING_INVOICE_NO + $invoice->id;
					$invoice->save();
					
					$journal_item->clear();
					$journal_item->journal_id = $journal->id;
					$journal_item->account_code = $accountCodesArray[CodesUtility::ACCOUNT_CODE_AR];
					$journal_item->cost = $value['cost'];
					$journal_item->accounting_type = CodesUtility::ACCOUNTING_TYPE_DEBIT;
					$journal_item->account_type = CodesUtility::ACCOUNT_TYPE_REGISTRATIONS;
					$journal_item->save();
					
					$journal_item->clear();
					$journal_item->journal_id = $journal->id;
					$journal_item->account_code =$accountCodesArray[CodesUtility::ACCOUNT_CODE_SALES];
					$journal_item->cost = $value['cost'];
					$journal_item->accounting_type = CodesUtility::ACCOUNTING_TYPE_CREDIT;
					$journal_item->account_type = CodesUtility::ACCOUNT_TYPE_REGISTRATIONS;
					$journal_item->save();
				}
				// details
				if($regcost > 0 ) {
					$cost = 0;
					if($regcost >= $value['month_cost']) {
						$cost = $value['month_cost'];
					} else {
						$cost = $regcost;
					}
					echo $cost;
				
					$registration->clear();
					$registration->accounting_journal_id = $journal->id;
					$registration->registration_id = $value['id'];
					$registration->registration_month_id = $value['registration_month_id'];
					$registration->accounting_invoice_id = $invoice->id;
					$registration->amount = $cost;
					$registration->save();
				}
				$registration_id = $value['id'];
				$regcost = $regcost - $value['month_cost'];
				
				
			}
			
		}
		
		
	}
	
	public function fixedScheduleAction(){
		
		Zend_Loader::loadClass("RegistrationChildDayTime");
		Zend_Loader::loadClass("RegistrationChildDayTimes");
		Zend_Loader::loadClass("Registration");
		Zend_Loader::loadClass("RegistrationChild");
		Zend_Loader::loadClass("QueryCreator");
		Zend_Loader::loadClass("BaseCatalog");
		Zend_Loader::loadClass("RegistrationDay");
		Zend_Loader::loadClass("RegistrationDays");
		Zend_Loader::loadClass("CatalogDay");
		Zend_Loader::loadClass("CatalogDays");
		Zend_Loader::loadClass("TimeUtility");
		Zend_Loader::loadClass("RegistrationMonth");
		Zend_Loader::loadClass("RegistrationMonths");
		Zend_Loader::loadClass("FamilyAccountPayer");
		Zend_Loader::loadClass("Payer");
		
		
		$query = new QueryCreator();
		$query->addSelect(" r.id ,r.catalog_session_id, max(scheduled_date) as max_end_date,bc.end_date,min(scheduled_date) as min_start_date,bc.start_date,r.family_account_id,lastname,firstname,bc.name");
		$query->addFrom(Registration::_tablename . " r ");
		$query->addJoin(" LEFT JOIN ". RegistrationChild::_tablename . " rc on rc.registration_id = r.id " );
		$query->addJoin(" LEFT JOIN ". RegistrationChildDayTime::_tablename . " rct on rct.registration_child_id = rc.id " );
		$query->addJoin(" LEFT JOIN ". BaseCatalog::_tablename . " bc on bc.id = r.catalog_session_id ");
		$query->addJoin(" LEFT JOIN ". FamilyAccountPayer::_tablename . " fap on fap.family_account_id = r.family_account_id and main_payer =1 " );
		$query->addJoin(" LEFT JOIN ". Payer::_tablename . " p on p.id= fap.payer_id ");
		$query->addWhere(" r.cancelled = 0 and r.deleted = 0 ");
		$query->addHaving("  datediff(max_end_date,bc.end_date) > 29 or datediff(bc.end_date,max_end_date) > 29 ");
		
		$query->addGroupBy(" r.id ");
		
		$db = DBCon::instance();
		$result = $db->executeQuery($query->createSQL());
		
		$regDay = new RegistrationDays();
		$schedDay = new CatalogDays();
		$regChildDayTimes = new RegistrationChildDayTimes();
		$regChildDayTime = new RegistrationChildDayTime();
		$regChildDayTimePrev = new RegistrationChildDayTime();
		$regChildId = new RegistrationChild();
		$regMonth = new RegistrationMonth();
		$regMonths = new RegistrationMonths();
		$registration = new Registration();
		foreach( $result as $key => $value ){
		
			$registration->changeRegistration(array('reg_id'=>$value['id'],'newCaresession'=>$value['catalog_session_id']));
		
		}
		
		
		
	}
	
	
  }
?>